<?php

namespace app\api\controller;

use think\Db;
use app\api\model\Cai as CaiModel;
use app\api\model\Yuyue as YuyueModel;

class Index
{
    public function index()
    {
        header('Location: /order');
        return;
    }

    public function test()
    {
        //return time();
        return md5('zq' . '163');

        Db::execute('insert into sb_ad (ad_name, ad_content ,status) values (1, "456",1)');


        return date('Y-m-d', strtotime('+1 day'));
        //return strtotime(date('Y-m-d H:i:s'));  //今天
        //return strtotime(date('Y-m-d', strtotime('+2 day')));

        return date('Y-m-d H:i:s', 1569381203);
        //return date('Y-m-d H:i:s', time()+24*12*60*60);


        /*
         * 1569427200  2019-09-26 00:00:00
         * 1569513600  2019-09-27 00:00:00
         */
    }

    public function sql()
    {
        //后台生成
        $arr = ['方静茹', '刘双峰', '卓梦奇', '李春节', '蒋政', '李汉高', '袁梦萍', '吴冬冬', '吴小芬', '郑云', '高茂银', '刘根', '蒋涛', '吴勤翰', '郑莹莹', '周美军', '翁金勇', '潘娟', '李海成', '史修桃', '秦玉迪', '颜磊刚', '王斯海', '蔡晨宇', '黄金星', '魏宁波', '周涛涛', '周燕', '陶永', '彭瑶', '覃伟', '徐海青', '马璐', '陈芸', '李敏', '晏铖', '范秀剑', '季昌东', '陈晨', '耿涛', '秦卫丽', '徐俊岭', '陈芬', '王志强', '杨锟', '刘源芬', '曹盛杰', '刘杨', '石炎', '谈盼盼', '项文倩', '林丽娟', '潘芳', '吴元财', '苑文杰', '李新凤', '严昌群'];

        for ($i = 106; $i < count($arr) + 106; $i++) {
            $md5info = md5('zq'. $i);

            echo "INSERT INTO `dc_630web_com`.`zq_user` (`id`, `username`, `password`, `jiaose`, `token`, `create_time`, `update_time`) VALUES ('" . $i . "', '" . $arr[$i - 106] . "', 'e10adc3949ba59abbe56e057f20f883e', '1', '" . $md5info . "', '1569458529', '1569458529');" . "<br>";
        }
    }

    //登录验证
    public function getUser()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $user = input('post.username');
        $pwd  = input('post.password');
        //file_put_contents('info.txt', $user . "\n", FILE_APPEND);
        //file_put_contents('info.txt', $pwd . "\n", FILE_APPEND);
        //file_put_contents('info.txt', var_export(input('post.'), true) . "\n", FILE_APPEND);

        $ret = Db::name('user')->where([
            'username' => $user,
            'password' => md5($pwd)
        ])->find();

        if ($ret) {
            show(1, '查询成功', $ret);
        } else {
            show(0, '账号密码错误!');
        }


    }

    //登录页验证cookie
    public function getUserCookie()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $user = input('post.username');
        $pwd  = input('post.password');
        //file_put_contents('info.txt', $user . "\n", FILE_APPEND);
        //file_put_contents('info.txt', $pwd . "\n", FILE_APPEND);
        //file_put_contents('info.txt', var_export(input('post.'), true) . "\n", FILE_APPEND);

        $ret = Db::name('user')->where([
            'username' => $user,
            'password' => $pwd
        ])->find();

        if ($ret) {
            show(1, '查询成功', $ret);
        } else {
            show(0, '账号密码错误!');
        }


    }

    //获取明天菜单
    public function getCai()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');

        //查询明天,  (大于明天凌晨, 小于后天凌晨)
        //$ret = Db::name('cai')->whereTime('yuyue_time', 'between', [date('Y-m-d', strtotime('+1 day')), date('Y-m-d', strtotime('+2 day'))])->find();
        $ret = Db::name('cai')->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))->find();
        //file_put_contents('info.txt', $ret . "\n", FILE_APPEND);

        if ($ret) {
            show(1, '查询成功', $ret);
        } else {
            show(0, '查询失败!');
        }
    }

    //上传明天菜单
    public function uploadCai()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $data = input('post.');
        // $data['tijiao']=1为新增 =2为修改.
        $data['yuyue_time'] = date('Y-m-d', strtotime('+1 day'));

        $cai = new CaiModel;
        //tijiao=1 为新增, tijiao=2 为修改
        if ($data['tijiao'] == 1) {
            $res = $cai->allowField(true)->save($data);
        } elseif ($data['tijiao'] == 2) {
            $res = $cai->allowField(true)->save($data, function ($query) {
                $query->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')));
            });
        }

        if (!$res) {
            return show(0, '添加失败!');
        } else {
            return show(1, '添加成功!');
        }

    }

    //通过token获取用户预约情况
    public function getYuyue()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $yuyue              = new YuyueModel;
        $data               = input('post.');
        $data['yuyue_time'] = date('Y-m-d', strtotime('+1 day'));

        $ret = Db::name('yuyue')
            ->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))
            ->where('user_token', 'eq', $data['user_token'])
            ->find();
        //查询是否已经点餐, 如果已经点餐, 把数据返回回去,如果没有点餐, 加一条记录
        if ($ret) {
            show(1, '查询成功', $ret);
        } else {
            $yuyue->save($data);
        }
    }

    //中午点餐
    public function setZhong()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $yuyue = new YuyueModel;
        $data  = input('post.');

        $res = $yuyue::where('user_token', 'eq', $data['user_token'])
            ->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))
            ->update([
                'yuyue_zhong' => $data['yuyue_zhong']
            ]);

        if ($res) {
            show(1, '操作成功!');
        } else {
            show(0, '操作超时!');
        }
    }

    //晚上点餐
    public function setWan()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $yuyue = new YuyueModel;
        $data  = input('post.');

        $res = $yuyue::where('user_token', 'eq', $data['user_token'])
            ->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))
            ->update([
                'yuyue_wan' => $data['yuyue_wan']
            ]);

        if ($res) {
            show(1, '操作成功!');
        } else {
            show(0, '操作超时!');
        }
    }

    //获取明天点餐人数
    public function getRenshu()
    {
        //解决跨域
        header('Access-Control-Allow-Origin:*');
        $zhong_renshu_m  = Db::name('yuyue')
            ->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))
            ->where('yuyue_zhong', 'eq', 2)
            ->count();
        $wan_renshu_m    = Db::name('yuyue')
            ->where('yuyue_time', 'eq', date('Y-m-d', strtotime('+1 day')))
            ->where('yuyue_wan', 'eq', 2)
            ->count();
        $zhong_renshu_j  = Db::name('yuyue')
            ->where('yuyue_time', 'eq', date('Y-m-d'))
            ->where('yuyue_zhong', 'eq', 2)
            ->count();
        $wan_renshu_j    = Db::name('yuyue')
            ->where('yuyue_time', 'eq', date('Y-m-d'))
            ->where('yuyue_wan', 'eq', 2)
            ->count();
        $data['zhong_m'] = $zhong_renshu_m;
        $data['wan_m']   = $wan_renshu_m;
        $data['zhong_j'] = $zhong_renshu_j;
        $data['wan_j']   = $wan_renshu_j;
        show(1, '查询成功', $data);
    }

}
